<template>
  <div class="material-library">
    <el-tabs type="border-card" v-model="activeName">
        <el-tab-pane label="金属材料" name="1">
            <div class="display-flex">
                <el-scrollbar class="block-left">
                    <el-tree :data="treeDatas" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
                </el-scrollbar>
                <div class="block-right">
                    <el-form label-position="right" label-width="100px" size="small">
                        <el-row :gutter="10">
                            <el-col :span="12">
                                <el-form-item label="牌号">
                                    <el-input v-model="datas.series" disabled/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="规格">
                                    <el-select v-model="datas.specification" style="width: 100%;">
                                        <el-option v-for="(item,index) in specificationOptions" :key="'specification'+index" :label="item" :value="item"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="10">
                            <el-col :span="12">
                                <el-form-item label="制品类型">
                                    <el-select v-model="datas.form" style="width: 100%;">
                                        <el-option v-for="(item,index) in formOptions" :key="'form'+index" :label="item" :value="item"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="热处理">
                                    <el-select v-model="datas.temper" style="width: 100%;">
                                        <el-option v-for="(item,index) in temperOptions" :key="'temper'+index" :label="item" :value="item"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="10">
                            <el-col :span="12">
                                <el-form-item label="厚度(公制)">
                                    <el-select v-model="datas.thickness" style="width: 100%;">
                                        <el-option v-for="(item,index) in thicknessOptions" :key="'thickness'+index" :label="item" :value="item"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="基准">
                                    <el-select v-model="datas.basis" style="width: 100%;">
                                        <el-option v-for="(item,index) in basisOptions" :key="'basis'+index" :label="item" :value="item"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="10" v-if="fields.length">
                            <el-col :span="12">
                                <el-form-item>
                                    <el-button type="primary" size="small" @click="handleQuery">查询</el-button>
                                    <el-button size="small" @click="handleReset">重置</el-button>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <el-table :data="tableDatas" v-if="fields.length" class="block-table" height=" calc(100vh - 370px)">
                        <el-table-column v-for="(item,index) in fields" :key="'table-column'+index" :prop="item.prop" :label="item.label" min-width="120" show-overflow-tooltip></el-table-column>
                    </el-table>
                </div>
            </div>
        </el-tab-pane>
        <el-tab-pane label="紧固件" name="2">
            <div class="display-flex">
                <el-scrollbar class="block-left">
                    <el-tree :data="treeDatas2" :props="defaultProps" @node-click="handleNodeClick2"></el-tree>
                </el-scrollbar>
                <div class="block-right">
                    <el-form label-position="right" label-width="100px" size="small">
                        <el-row :gutter="10">
                            <el-col :span="12">
                                <el-form-item label="紧固件">
                                    <el-input v-model="datas2.type" disabled/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="名称">
                                    <el-select v-model="datas2.name" style="width: 100%;">
                                        <el-option v-for="(item,index) in nameOptions" :key="'temper'+index" :label="item" :value="item"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item>
                                    <el-button type="primary" size="small" @click="handleQuery2">查询</el-button>
                                    <el-button size="small" @click="handleReset2">重置</el-button>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <el-table :data="tableDatas2" v-if="fields2.length" class="block-table" height=" calc(100vh - 260px)">
                        <el-table-column v-for="(item,index) in fields2" :key="'table-column'+index" :prop="item.prop" :label="item.label" min-width="120" show-overflow-tooltip></el-table-column>
                    </el-table>
                </div>
            </div>
        </el-tab-pane>
    </el-tabs>
    
  </div>
</template>

<script>
import {getMaterialCategory,getMaterialQueryOption,queryMaterials,getFastenerCategory,getFastenerQueryOption,queryFasteners} from '@/api/materialLibrary.js'
export default {
    name:'MaterialLibrary',
    data () {
        return {
            activeName: '1',
            treeDatas:[],
            defaultProps: {
                children: 'children',
                label: 'label'
            },
            datas:{
                type:'',
                series: '',
                basis: '',
                form:'',
                specification: '',
                temper:'',
                thickness: ''
            },
            basisOptions: [],
            formOptions: [],
            specificationOptions: [],
            temperOptions: [],
            thicknessOptions: [],
            fields: [],
            tableDatas: [],
            treeDatas2:[],
            datas2:{
                name:'',
                type: ''
            },
            nameOptions: [],
            fields2: [],
            tableDatas2: [],
        }
    },
    mounted(){
        this.initData()
        this.initData2()
    },
    methods:{
        initData(){
            getMaterialCategory().then(res => {
                this.treeDatas = res.data.map(item => {
                    return {
                        label:item.type,
                        children: item.series.map(el => {
                            return {
                                label: el,
                                parent:item.type
                            }
                        })
                    }
                })
            })
        },
        initData2(){
            getFastenerCategory().then(res => {
                this.treeDatas2 = res.data.type.map(item => {
                    return {
                        label:item
                    }
                })
            })
        },
        handleQuery(){
            queryMaterials(this.datas).then(res => {
                const newDatas = res.data
                this.fields = []
                newDatas.columns.forEach((element,index) => {
                    const newObj = {
                        prop:element,
                        label: newDatas.titles[index]
                    }
                    this.fields.push(newObj)
                });
                this.tableDatas = newDatas.data
            })
        },
        handleReset(){
            this.datas.basis = ''
            this.datas.form = ''
            this.datas.specification = ''
            this.datas.temper = ''
            this.datas.thickness = ''
            this.handleQuery()
        },
        handleNodeClick(data){
            if(data.parent && data.label){
                const obj = {
                    type: data.parent,
                    series:data.label
                }
                getMaterialQueryOption(obj).then(res => {
                    const newData = res.data
                    this.basisOptions = newData.basis
                    this.formOptions = newData.form
                    this.specificationOptions = newData.specification
                    this.temperOptions = newData.temper
                    this.thicknessOptions = newData.thickness
                    this.datas.series = data.label
                    this.datas.type = data.parent
                    this.handleQuery()
                })
            }
            
        },
        handleQuery2(){
            const obj = {
                name:this.datas2.name
            }
            queryFasteners(obj).then(res => {
                const newDatas = res.data
                this.fields2 = []
                newDatas.columns.forEach((element,index) => {
                    const newObj = {
                        prop:element,
                        label: newDatas.titles[index]
                    }
                    this.fields2.push(newObj)
                });
                this.tableDatas2 = newDatas.data
            })
        },
        handleReset2(){
            this.datas.name = this.nameOptions.length? this.nameOptions[0]:''
            this.handleQuery2()
        },
        handleNodeClick2(data){
            if(data.label){
                const obj = {
                    type:data.label
                }
                getFastenerQueryOption(obj).then(res => {
                    const newData = res.data
                    this.nameOptions = newData.name
                    this.formOptions = newData.form
                    this.datas2.type = data.label
                    if(this.nameOptions.length){
                        this.datas2.name = this.nameOptions[0]
                         this.handleQuery2()
                    }
                    //
                })
            }
        }
        
    }

}
</script>

<style  scoped>
.display-flex{
    display: flex;
}
.block-left{
    width: 300px;
    height: calc(100vh - 160px);
    border-right: 1px solid #e5e5e5;
}
.block-right{
    width: calc(100% - 300px);
}
.block-top{
    margin-left: 12px;
    margin-top: 12px;
    padding: 12px 12px 0 12px;
    border: 1px solid #e5e5e5;
}
.block-table{
    padding: 0px 12px 12px 12px;
}
:deep(.el-scrollbar__wrap){
    overflow-x: hidden;
}
</style>